/*
pager V.1
- by WR
*/
(function ($) {

    jQuery.fn.pager = function (settings) {

        var defaults = {
            caption: "Page : ",
            firstTitle: "Go to first page",
            lastTitle: "Go to last page",
            prevTitle: "Go to previous page",
            nextTitle: "Go to next page",
            showMaxPage: true,
            showTotalItems: true,
            maxPageCaption: "Total pages",
            totalCaption: "Total items",
            totalItems: "0",
            currentPage: 1,
            maxPage: 10,
            maxRange: 10,
            pageSize: 0,
            onPaging: function () { return true }
        };

        var options = jQuery.extend(defaults, settings);

        var holder = $(this);
        var currentPage = options.currentPage;
        if (currentPage == null) { currentPage = 1 }

        //set paging width
        //$(holder).width($(holder).find("table:first").width());
        //add paging Markup
        $(holder).next("div.paging").remove();

        if (options.maxPage > 0) {
            //if (options.pageSize > 0) {
            var pageSizeConfig = '10,20,30,50,100';
            var arrPageSizeConfig = pageSizeConfig.split(",");
            var pageSelection = '<select class="pageSize" style="height:18px;margin:0px 20px 0px 20px;">';
            for (var i = 0; i < arrPageSizeConfig.length; i++) {
                if (arrPageSizeConfig[i] == options.pageSize) {
                    pageSelection += '<option value="' + arrPageSizeConfig[i] + '" selected>' + arrPageSizeConfig[i] + '</option>';
                } else {
                    pageSelection += '<option value="' + arrPageSizeConfig[i] + '">' + arrPageSizeConfig[i] + '</option>';
                }
            }
            pageSelection += '</select>';

            //} else {
            //var pageSelection = '';
            //}

            $(holder).after('<div class="paging ui-corner-bottom ui-helper-clearfix" style=" padding:2px;height:20px"><span style="float:left">' + options.caption + '</span><span class="firstPage ui-icon ui-icon-seek-first" style="cursor:pointer;float:left" title="' + options.firstTitle + '"></span><span class="prevPage ui-icon ui-icon-seek-prev" style="cursor:pointer; float:left; margin-left:5px" title="' + options.prevTitle + '"></span><div class="pageNum" style="float:left"></div><span class="nextPage ui-icon ui-icon-seek-next" style="cursor:pointer; float:left; margin-left:5px" title="' + options.nextTitle + '"></span><span class="lastPage ui-icon ui-icon-seek-end" style="cursor:pointer; float:left; margin-left:5px" title="' + options.lastTitle + '"></span>' + pageSelection + '</div>');

            //
            var divpaging = $(holder).next("div.paging"); //$("div[pagingOf="+$(holder).attr("id")+"]");
            //add page number
            /*for (i = 1; i <= options.maxPage; i++) {
            if (i == currentPage) { thisWeight = ";font-weight:bold" } else { thisWeight = ";font-weight:normal" }

            $(divpaging).find(".pageNum").append('<span style="cursor:pointer;text-decoration:underline;float:left; margin-left:5px' + thisWeight + '">' + i + '</span>');

            $(divpaging).find(".pageNum span:last").click(function() {

            var thisNum = $(this);
            var thisPage = $(this).text();
            if ($(this).text() != currentPage) { gotoPage(thisNum, thisPage, "pageNum"); }

            });
            }*/
            var ceilrangePage = Math.ceil(options.maxRange / 2)
            var floorrangePage = Math.floor(options.maxRange / 2)
            var pagelastMax = options.maxPage - floorrangePage
            var floorR = options.maxPage - floorrangePage
            var t1 = options.maxPage - (options.maxRange - 1)
            var thisWeight;

            if (options.maxPage > options.maxRange) {
                if (currentPage <= ceilrangePage) {
                    for (i = 1; i <= options.maxRange; i++) {
                        if (i == currentPage) { thisWeight = ";font-weight:bold" } else { thisWeight = ";font-weight:normal" }
                        $(divpaging).find(".pageNum").append('<span style="cursor:pointer;text-decoration:underline;float:left; margin-left:5px' + thisWeight + '">' + i + '</span>');
                        $(divpaging).find(".pageNum span:last").click(function () {
                            var thisNum = $(this);
                            var thisPage = $(this).text();
                            gotoPage(thisNum, thisPage, "pageNum");
                        });
                    }
                    $(divpaging).find(".pageNum").append('<span class="point" style="float:left; margin-left:5px' + thisWeight + '">...</span>');
                } else if (ceilrangePage < currentPage && currentPage <= floorR) {
                    var f1 = Number(currentPage) - floorrangePage
                    var f2 = Number(currentPage) + floorrangePage
                    var f3 = options.maxRange % 2
                    if (f3 == 0) {
                        $(divpaging).find(".pageNum").append('<span style="float:left; margin-left:5px' + thisWeight + '">...</span>');
                        for (i = f1; i <= f2 - 1; i++) {
                            if (i == currentPage) { thisWeight = ";font-weight:bold" } else { thisWeight = ";font-weight:normal" }
                            $(divpaging).find(".pageNum").append('<span style="cursor:pointer;text-decoration:underline;float:left; margin-left:5px' + thisWeight + '">' + i + '</span>');
                            $(divpaging).find(".pageNum span:last").click(function () {
                                var thisNum = $(this);
                                var thisPage = $(this).text();
                                gotoPage(thisNum, thisPage, "pageNum");
                            });
                        }
                        $(divpaging).find(".pageNum").append('<span style="float:left; margin-left:5px' + thisWeight + '">...</span>');
                    } else {
                        $(divpaging).find(".pageNum").append('<span style="float:left; margin-left:5px' + thisWeight + '">...</span>');
                        for (i = f1; i <= f2; i++) {
                            if (i == currentPage) { thisWeight = ";font-weight:bold" } else { thisWeight = ";font-weight:normal" }
                            $(divpaging).find(".pageNum").append('<span style="cursor:pointer;text-decoration:underline;float:left; margin-left:5px' + thisWeight + '">' + i + '</span>');
                            $(divpaging).find(".pageNum span:last").click(function () {
                                var thisNum = $(this);
                                var thisPage = $(this).text();
                                gotoPage(thisNum, thisPage, "pageNum");
                            });
                        }
                        if (currentPage < pagelastMax) {
                            $(divpaging).find(".pageNum").append('<span style="float:left; margin-left:5px' + thisWeight + '">...</span>');
                        }
                    }
                } else if (currentPage > floorR) {
                    $(divpaging).find(".pageNum").append('<span style="float:left; margin-left:5px' + thisWeight + '">...</span>');
                    for (i = t1; i <= options.maxPage; i++) {
                        if (i == currentPage) { thisWeight = ";font-weight:bold" } else { thisWeight = ";font-weight:normal" }
                        $(divpaging).find(".pageNum").append('<span style="cursor:pointer;text-decoration:underline;float:left; margin-left:5px' + thisWeight + '">' + i + '</span>');
                        $(divpaging).find(".pageNum span:last").click(function () {
                            var thisNum = $(this);
                            var thisPage = $(this).text();
                            gotoPage(thisNum, thisPage, "pageNum");
                        });
                    }
                }

            } else {

                for (i = 1; i <= options.maxPage; i++) {
                    if (i == currentPage) { thisWeight = ";font-weight:bold" } else { thisWeight = ";font-weight:normal" }
                    $(divpaging).find(".pageNum").append('<span style="cursor:pointer;text-decoration:underline;float:left; margin-left:5px' + thisWeight + '">' + i + '</span>');
                    $(divpaging).find(".pageNum span:last").click(function () {
                        var thisNum = $(this);
                        var thisPage = $(this).text();
                        gotoPage(thisNum, thisPage, "pageNum");
                    });
                }
            }
            //show Maxpage
            if (options.showMaxPage) {
                $(divpaging).append('<span style="float:left">' + options.maxPageCaption + ' : ' + options.maxPage + '</span>')
            }
            //show Items
            if (options.showTotalItems) {
                $(divpaging).append('<span style="float:left;padding:0px 0px 0px 20px">' + options.totalCaption + ' : ' + options.totalItems + '</span>')
            }
            //maxrange
            /*if (options.currentPage < options.maxRange && options.maxPage > options.maxRange) {
            $(divpaging).find(".pageNum span:gt(" + (options.maxRange - 1) + ")").hide();
            $(divpaging).find(".pageNum").append('<span class="pagesplit" style="float:left">...</span>');
            } else if (options.currentPage >= options.maxRange && options.currentPage < Number(options.maxPage) - Number(options.maxRange) + 1) {
            //var thisRange = Math.ceil(options.currentPage/options.maxRange);
            var maxHide = Number(options.currentPage) + Number(options.maxRange / 2) - 1
            var minHide = Number(options.currentPage) - Number(options.maxRange / 2)
            //alert(minHide+":"+maxHide)
            $(divpaging).find(".pageNum span:gt(" + maxHide + ")").hide();
            $(divpaging).find(".pageNum span:lt(" + minHide + ")").hide();
            $(divpaging).find(".pageNum").append('<span class="pagesplit" style="float:left">...</span>').prepend('<span class="pagesplit" style="float:left">...</span>');
            } else if (options.maxPage > options.maxRange) {
            $(divpaging).find(".pageNum span:lt(" + (Number(options.maxPage) - Number(options.maxRange)) + ")").hide();
            $(divpaging).find(".pageNum").prepend('<span class="pagesplit" style="float:left">...</span>');
            }*/
            //
            //end page number
            //go to first Page
            $(divpaging).find(".firstPage").click(function () {
                var thisNum = $(this);
                var thisPage = 1;
                gotoPage(thisNum, thisPage, "firstPage");
            });
            //go to last Page
            $(divpaging).find(".lastPage").click(function () {
                var thisNum = $(this);
                var thisPage = options.maxPage;
                gotoPage(thisNum, thisPage, "lastPage");
            });
            //go to next Page
            $(divpaging).find(".nextPage").click(function () {
                var thisNum = $(this);
                var thisPage = Number(currentPage) + 1;
                if (parseFloat(currentPage) < parseFloat(options.maxPage)) {
                    gotoPage(thisNum, thisPage, "nextPage");
                };
            });
            //go to prev Page
            $(divpaging).find(".prevPage").click(function () {

                var thisNum = $(this);
                var thisPage = Number(currentPage) - 1;
                if (parseFloat(currentPage) > 1) { gotoPage(thisNum, thisPage, "prevPage"); };
            });
            //});
            //show data by pageSize
            $(divpaging).find(".pageSize").change(function () {
                var thisNum = $(this);
                var thisPage = 1;
                
                if ($(holder).parents("div").prev(".attrGrid:first").size() > 0) {
                    $(holder).parents("div").prev(".attrGrid:first").attr("pageSize", $(this).val());
                }
                //$("body").data("pageSize", ($(divpaging).find(".pageSize").val() == null) ? 0 : $(divpaging).find(".pageSize").val());
                gotoPage(thisNum, thisPage, "firstPage");
            });



            function gotoPage(obj, pageNum, displayType) {
                //options.onPaging(pageNum);
                if (options.onPaging(pageNum) == false) { return false }
                $(holder).html('<div style="padding:10px"><img src="Content/Images/loading.gif" alt="loading" align="absmiddle"/>Opening page please wait...</div>');
                $(divpaging).find(".pageNum span").css({ 'font-weight': 'normal' });

                switch (displayType) {

                    case 'firstPage':
                        $(divpaging).find(".pageNum span:first").css({ 'font-weight': 'bold' });
                        break;

                    case 'lastPage':
                        $(divpaging).find(".pageNum span:last").css({ 'font-weight': 'bold' });
                        break;

                    case 'nextPage':
                        $(divpaging).find(".pageNum span:not(.pagesplit):eq(" + currentPage + ")").css({ 'font-weight': 'bold' }); ;
                        break;

                    case 'prevPage':
                        $(divpaging).find(".pageNum span:not(.pagesplit):eq(" + (currentPage - 2) + ")").css({ 'font-weight': 'bold' });
                        break;

                    default:
                        $(obj).css({ 'font-weight': 'bold' });
                        break;
                }

                currentPage = pageNum;
                $("body").data("currentPage", (currentPage == null) ? 0 : currentPage);
            }

        } //end check maxrow

    } //end plug-in



})(jQuery);	